package hbase;

import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.Pair;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;

/**
 * Created by spark on 16-10-20.
 */
public class HTableTest extends Configured  implements Tool {

    public void printTableRegions(HTable table) throws IOException {
        System.out.println("print table regions fro " + table.getName().getNameAsString());
        Pair<byte[][],byte[][]> pair = table.getRegionLocator().getStartEndKeys();
        for (int n = 0; n < pair.getFirst().length; n++) {
            byte[] start = pair.getFirst()[n];
            byte[] end = pair.getSecond()[n];
            System.out.println("[" + (n + 1) + "] "
                    + "start key = " + (start.length == 8 ? Bytes.toLong(start) : Bytes.toString(start))
                    + " / end key = " + (end.length == 8 ? Bytes.toLong(end) : Bytes.toString(end)));
        }

    }

    @Override
    public int run(String[] args) throws Exception {

        Connection conn = ConnectionFactory.createConnection(getConf());
        Admin admin = conn.getAdmin();

//        HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf("test"));
//        tableDescriptor.addFamily(new HColumnDescriptor("info"));
//        admin.createTable(tableDescriptor, Bytes.toBytes(1L), Bytes.toBytes(100L), 10);
//        printTableRegions((HTable) conn.getTable(TableName.valueOf("test")));
//
//        tableDescriptor = new HTableDescriptor(TableName.valueOf("test2"));
//        tableDescriptor.addFamily(new HColumnDescriptor("info"));
//        byte[][] splits = new byte[][] {
//            Bytes.toBytes("A"),
//            Bytes.toBytes("G"),
//            Bytes.toBytes("Q"),
//            Bytes.toBytes("T"),
//            Bytes.toBytes("W")
//        };
//        admin.createTable(tableDescriptor, splits);
//        printTableRegions((HTable) conn.getTable(TableName.valueOf("test2")));


        for (HRegionInfo info:
                admin.getTableRegions(TableName.valueOf("test"))) {
            admin.unassign(info.getRegionName(), false);
//            info.getRegionName()
        }

        ClusterStatus clusterStatus = admin.getClusterStatus();
        clusterStatus.getRegionsCount();

        ServerLoad load = clusterStatus.getLoad(clusterStatus.getServers().iterator().next());
        load.getStorefileSizeInMB();
        load.getRegionServerCoprocessors();
        load.getRootIndexSizeKB();

        RegionLoad next = load.getRegionsLoad().values().iterator().next();
        next.getNameAsString();
        next.getStores();


        return 0;
    }

    public static void main(String[] args) throws Exception {
        int exitCode = ToolRunner.run(new HTableTest(), args);
        System.exit(exitCode);
    }
}
